package java6703.lesson06;

import java.util.Scanner;
import java.util.TreeMap;

public class Counter6703 {
    public static void main(String[] args) {
        System.out.print("请输入字符串:");

        //键盘录入字符串
        Scanner scanner = new Scanner(System.in);

        //创建字符串对象，存储键盘输入的值
        String line = scanner.nextLine();

        TreeMap<Character, Integer> cj = count6703(line);

        //输出结果
        System.out.print("包含:");
        cj.forEach((k, v) -> System.out.print("字符"+k + "=" + v + " "));
    }

    private static TreeMap<Character, Integer> count6703(String line) {
        TreeMap<Character, Integer> cj = new TreeMap<Character, Integer>();
        char[] ch = line.toCharArray();

        //判断获取到的value为空，设置初始值1
        for (char c : ch) {
            Integer i = cj.get(c);
            if (i == null) {
                cj.put(c, 1);
            }else{
                //使用jdk8的新特性进行计算
                cj.put(c,++i);
            }
        }
        return cj;
    }
}
